package bizlogic.bookinfo;

import java.util.ArrayList;
import java.util.List;

import bizlogic.bookinfo.domain.BookInfo;
import dao.BookInfoDao;
import dao.BorrowInfoDao;
import utils.StringUtil;


public class BookInfoService
{
    BookInfoDao bookInfoDao = new BookInfoDao();

    /*
     * 添加图书信息
     */
    public String addBookInfo(BookInfo bookInfo) {
        if (bookInfoDao.CheckExitsAdd(bookInfo.getBookname(), bookInfo.getAuthor())) {
            return "图书已存在";
        } else {
            int addBookInfo = bookInfoDao.addBookInfo(bookInfo);
            if (addBookInfo == 1) {
                return "添加成功�";
            } else {
                return "添加失败";
            }
        }
    }

    /*
     * 更新图书信息
     */
    public String updateBookInfo(BookInfo bookInfo) {

        BookInfo oldBookInfo = bookInfoDao.queryBookInfoByID(bookInfo.getBookid());
        String oldbookname = oldBookInfo.getBookname();
        if (bookInfoDao.CheckExitsEdit(oldbookname, bookInfo.getBookname(), bookInfo.getAuthor())) {
            return "图书已存在";
        } else {
            int updateBookInfo = bookInfoDao.updateBookInfo(bookInfo);
            if (updateBookInfo == 1) {
                return "更新成功";
            } else {
                return "更新失败";
            }
        }

    }

    /*
   /*
     * 删除图书信息（传入单个图书id删除单条数据）
     */
    public String deleteBookInfoByBookId(String bookIds) {
        List<String> deleteSuccess = new ArrayList<String>();
        List<String> deleteFail = new ArrayList<String>();
        List<String> deleteError = new ArrayList<String>();
        String[] strings = bookIds.split(",");
        for (String bookid : strings) {
            BookInfo queryBookInfoByID = bookInfoDao.queryBookInfoByID(bookid);
            BorrowInfoDao borrowInfoDao = new BorrowInfoDao();
         // 判断是否归还
            if (borrowInfoDao.Noreturn(bookid)) {
             // 没归还
                deleteFail.add(queryBookInfoByID.getBookname());
            } else {
             // 归还，删除
                int m = bookInfoDao.deleteBookinfoByBookId(bookid);
                if (m > 0) {
                    deleteSuccess.add(queryBookInfoByID.getBookname());
                } else {
                    deleteError.add(queryBookInfoByID.getBookname());
                }

            }
        }
        StringBuffer resultStringBuffer = new StringBuffer();
        if (!deleteSuccess.isEmpty()) {
            resultStringBuffer.append(StringUtil.JoinString(deleteSuccess, "��")).append("ɾ���ɹ�</br>");
        }
        if (!deleteFail.isEmpty()) {
            resultStringBuffer.append(StringUtil.JoinString(deleteFail, "��")).append("ͼ��û�黹��ɾ��ʧ�ܣ�</br>");
        }
        if (!deleteError.isEmpty()) {
            resultStringBuffer.append(StringUtil.JoinString(deleteError, "��")).append("���ִ���</br>");
        }
        return resultStringBuffer.toString();
    }

    /*
     * 根据图书id查询图书信息
     */
    public BookInfo queryBookInfoByID(String bookId) {
        return bookInfoDao.queryBookInfoByID(bookId);
    }

    /*
     * 查询图书信息列表（返回查询的实体集合）
     */
    public List<BookInfo> queryBookinfoByKeyWords(int pageIndex, int pageSize, String bookName, Integer bookType) {
        return bookInfoDao.queryBookinfoByKeyWords(pageIndex, pageSize, bookName, bookType);
    }

    /*
     * 查询图书信息表中所有数据的条数
     */
    public int findLength() {
        return bookInfoDao.findLength();
    }


}
